package com.rzcoding.util;

import okhttp3.*;

import java.io.IOException;

public class HttpClient {
    private static final OkHttpClient client = new OkHttpClient();
    private static final MediaType parse = MediaType.parse("application/json;charset=utf-8");

    //Get方法调用服务
    public static String httpGet(String url) throws IOException {
        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();// 返回的是string 类型
    }

    //Post方法调用服务
    public static String httpPost4Json(String url, String content) throws IOException {
        RequestBody requestBody = RequestBody.create(parse, content);
        Request request = new Request.Builder()
                .url(url)
                .post(requestBody)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }


    public static String httpPost4form(String url, FormBody formBody) throws Exception {
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Response response = client.newCall(request).execute();
        return response.body().string();
    }
}
